package com.allen.retrofit2_rxjava2.view.selectImage.adapter;

import android.content.Context;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;

import com.allen.retrofit2_rxjava2.R;
import com.allen.retrofit2_rxjava2.view.selectImage.entity.ChooseUploadParam;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lierdong on 2016/12/6.
 */
public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.GalleryViewHolder> {

    private static final String TAG = "gallery";

    private Context context;
    private List<ChooseUploadParam> params;
    private UploadVideoImageAdapter.OnItemClickListener onItemClickListener;
    private OnItemCheckedListener onItemCheckedListener;
    private OnItemCblickListener onItemCblickListener;

    private SparseBooleanArray cbArray;

    public GalleryAdapter(Context context, List<ChooseUploadParam> params) {
        this.context = context;
        this.params = params;
        initCbArray();
    }

    private void initCbArray() {
        cbArray = new SparseBooleanArray(params.size());
        for (int i = 0; i < params.size(); i++) {
            cbArray.append(i, false);
        }
    }

    @Override
    public GalleryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = View.inflate(context, R.layout.item_rv_gallery, null);
        return new GalleryViewHolder(itemView);
    }

    public void setOnItemClickListener(UploadVideoImageAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemCheckedListener(OnItemCheckedListener onItemCheckedListener) {
        this.onItemCheckedListener = onItemCheckedListener;
    }

    public void setOnItemCblickListener(OnItemCblickListener onItemCblickListener) {
        this.onItemCblickListener = onItemCblickListener;
    }

    @Override
    public void onBindViewHolder(GalleryViewHolder holder, int position) {
        ChooseUploadParam param = params.get(position);
        String realPath = param.getPath();
        switch (param.getType()) {
            case UploadVideoImageAdapter.TYPE_IMG:
//                holder.iv_isVideo.setVisibility(View.GONE);
                break;
            case UploadVideoImageAdapter.TYPE_VIDEO:
//                holder.iv_isVideo.setVisibility(View.VISIBLE);
                String suffix = getSuffix(realPath);
                realPath = realPath.replace(suffix, ".jpg");
                break;
        }
        holder.cb_item.setChecked(cbArray.get(position));
        if (cbArray.get(position)) {
            holder.shadow.setVisibility(View.VISIBLE);
        } else {
            holder.shadow.setVisibility(View.GONE);
        }
        Glide.with(context.getApplicationContext())
                .load(realPath)
                .apply(new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .skipMemoryCache(true)
                        .error(R.mipmap.banner_default)
                        .centerCrop())
                .into(holder.iv_content);
    }

    @Override
    public int getItemCount() {
        return params.size();
    }

    class GalleryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

        public CheckBox cb_item;
        //        public ImageView iv_isVideo;
        public ImageView iv_content;
        public View shadow;

        public GalleryViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            cb_item = (CheckBox) itemView.findViewById(R.id.cb_item);
            cb_item.setOnCheckedChangeListener(this);
            cb_item.setOnClickListener(this);
//            iv_isVideo = (ImageView) itemView.findViewById(R.id.iv_isVideo);
            iv_content = (ImageView) itemView.findViewById(R.id.iv_content);
            iv_content.setOnClickListener(this);
            shadow = itemView.findViewById(R.id.shadow);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_content:
                    if (onItemCblickListener != null) {
                        int position = getLayoutPosition();
                        ChooseUploadParam param = params.get(position);
                        if (cb_item.isChecked()) {
                            cb_item.setChecked(false);
                        } else {
                            cb_item.setChecked(true);
                        }
                        onItemCblickListener.onItemCbClick(position, param, cb_item);
                    }
                    break;
                default:
                    if (onItemClickListener != null) {
                        int position = getLayoutPosition();
                        ChooseUploadParam param = params.get(position);
                        onItemClickListener.onItemClick(position, param.getType(), param);
                    }
                    break;
            }
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            cbArray.put(getLayoutPosition(), isChecked);
            if (isChecked) {
                shadow.setVisibility(View.VISIBLE);
            } else {
                shadow.setVisibility(View.GONE);
            }
        }
    }

    private String getSuffix(String path) {
        return path.substring(path.indexOf("."));
    }

    public interface OnItemCheckedListener {

        void onItemChecked(int position, ChooseUploadParam param, boolean isChecked);
    }

    public interface OnItemCblickListener {

        void onItemCbClick(int position, ChooseUploadParam param, CheckBox cb);
    }

    public boolean hasVideo() {
        for (ChooseUploadParam param : params) {
            if (param.getType() == UploadVideoImageAdapter.TYPE_VIDEO) {
                return true;
            }
        }
        return false;
    }

    public void setCbSelected(ArrayList<ChooseUploadParam> selectedParams) {
        for (int i = 0; i < params.size(); i++) {
            ChooseUploadParam param = params.get(i);
            cbArray.put(i, inParams(param, selectedParams)); //如果有相同的地址 选中框设为true
        }
        notifyDataSetChanged();
    }

    public boolean inParams(ChooseUploadParam param, ArrayList<ChooseUploadParam> selectedParams) {
        for (ChooseUploadParam chooseUploadParam : selectedParams) {
            if (param.getPath().equals(chooseUploadParam.getPath())) {
                return true;
            }
        }
        return false;
    }

}
